package com.sxt.system.controller;

import com.sxt.system.common.Constast;
import com.sxt.system.common.DataGridView;
import com.sxt.system.config.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UploadController
 * @Description TODO
 * @Author 雷哥
 * @Date 2020/3/8 14:09
 */
@RestController
@RequestMapping(Constast.API+"upload")
public class UploadController extends  BaseController{

    @Autowired
    private UploadService uploadService;

    /**
     * 文件上传
     */
    @PostMapping("doUpload")
    public DataGridView uploadFile(MultipartFile[] mfs){
        DataGridView res = new DataGridView();
        if(null!=mfs&&mfs.length>0){
            res.setCode(0);
           List<String> urls=new ArrayList<>(mfs.length);
            for (MultipartFile mf : mfs) {
                String path = this.uploadService.uploadImage(mf);
                urls.add(path);
            }
            res.setData(urls);
            return  res;
        }else{
            res.setCode(-1);
            res.setMsg("上传文件出错，选择文件为空");
            return res;
        }
    }

}
